<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use think\Validate;
use think\Db;
use app\admin\model\Coupon;
use app\admin\model\CtmCun;


class CouponController extends CommonController {
    /*
     * 代金券列表
     * author xfwed 
     * date 2019.09.16
     */

    public function couponList() {
        $issue = input('param.issue');
        $perPage = input('param.perPage');
        $page = input('param.page');
        $type = input('param.type');
        $if_get = input('param.if_get');//用户主动领取 0全部1是2否
        $ctm_type = input('param.ctm_type');//优惠券使用范围（用户身份 多选） 1仅新人可用2仅普通用户可用3仅特权会员可用
        $give_type = input('param.give_type');//0全部1.普通获得 2下单赠送 3平台发放	

        $issue = isset($issue) ? $issue : 0;
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        if ($issue) {
            $where['issue'] = $issue;
        }
        if ($type) {
            $where['type'] = $type;
        }
        if($if_get){
            $where['if_get'] = $if_get;
        }
        if($ctm_type){
            $where['ctm_type'] = $ctm_type;
        }
        if($give_type){
            $where['give_type'] = $give_type;
        }
        $where = isset($where) ? $where : [];
        $couponList = Coupon::where($where)
                ->paginate($perPage);
        return $this->ajaxReturn(0, '查询成功', $couponList);
    }
    
    /*
     * 添加代金券
     * author xfwed 
     * date 2019.09.16
     */

    public function addCoupon() {
        $rule = [
            'name' => 'require',
            'money' => 'require',
            'reach' => 'require',
            'start_time' => 'require',
            'mature' => 'require',
            'number' => 'require',
            'issue' => 'require',
            'type' => 'require',
            'if_get' => 'require',
            'ctm_type' => 'require',
            'give_type' => 'require',
        ];
        $msg = [
            'name.require' => '代金券名称必须',
            'money.require' => '代金券金额必须',
            'reach.require' => '使用条件必须',
            'start_time.require' => '开始时间必须',
            'mature.require' => '到期时间必须',
            'number.require' => '发行量必须',
            'issue.require' => '是否发布必须',
            'type.require' => '代金券类型必须',
            'if_get.require' => '用户主动领取必须',
            'ctm_type.require' => '使用范围（用户身份）必须',
            'give_type.require' => '获得方式必须',
        ];
        $all = input('param.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }
        if($all['reach']<$all['money']){
            return $this->ajaxReturn(3, '使用范围应该大于等于代金券金额', null);
        }

        $data['type'] = $all['type'];
        $data['coupontype_id'] = 0;
        $data['name'] = $all['name'];
        $data['money'] = $all['money'];
        $data['reach'] = $all['reach'];
        $data['start_time'] = $all['start_time'];
        $data['mature'] = $all['mature'];
        $data['number'] = $all['number'];
        $data['issue'] = $all['issue'];
        $data['add_time'] = date('Y-m-d H:i:s');
        $data['if_get'] = $all['if_get'];
        $data['ctm_type'] = $all['ctm_type'];
        $data['give_type'] = $all['give_type'];
        $insertId = Coupon::insertGetId($data);
        if ($insertId) {
            return $this->ajaxReturn(0, '添加成功', null);
        } else {
            return $this->ajaxReturn(3, '添加失败', null);
        }
    }
    
    /*
     * 发布代金券
     * author xfwed 
     * date 2019.09.16
     */

    public function issueCoupon() {
        $rule = [
            'id' => 'require',
        ];
        $msg = [
            'id.require' => '代金券id必须',
        ];
        $all = input('param.');
        $validate = new Validate($rule, $msg);
        
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }
        
        $id = $all['id'];

        $couponRow = Coupon::where('id','=',$id)->find();
        if($couponRow['issue'] == 1){
            return $this->ajaxReturn(3, '已经发布啦', null);
        }
        $save = Coupon::where('id','=',$id)->update(['issue'=>1]);
        if ($save) {
            return $this->ajaxReturn(0, '发布成功', null);
        } else {
            return $this->ajaxReturn(3, '发布失败', null);
        }
    }
    
    /*
     * 领取代金券用户列表
     * author xfwed 
     * date 2019.09.16
     */

    public function couponCtmList() {
        $id = input('param.id');
        $employ = input('param.employ');
        $perPage = input('param.perPage');
        $page = input('param.page');

        $employ = isset($employ) ? $employ : 0;
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        if ($employ) {
            $where['c.employ'] = $employ;
        }
        $where['c.cun_id'] = $id;
        $ctmCunList = CtmCun::alias('c')
                ->join('custom t','t.id = c.ctm_id','left')
                ->where($where)
                ->field('c.employ,c.add_time,c.end_time,t.custom_phone,t.custom_name')
                ->paginate($perPage);
        return $this->ajaxReturn(0, '查询成功', $ctmCunList);
    }
    
    /*
     * 下架代金券
     * author xfwed 
     * date 2019.12.02
     */

    public function soldOutCoupon() {
        $rule = [
            'id' => 'require',
        ];
        $msg = [
            'id.require' => '代金券id必须',
        ];
        $all = input('param.');
        $validate = new Validate($rule, $msg);
        
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }
        
        $id = $all['id'];

        $couponRow = Coupon::where('id','=',$id)->find();
        if($couponRow['issue'] == 2){
            return $this->ajaxReturn(3, '已经下架啦', null);
        }
        $save = Coupon::where('id','=',$id)->update(['issue'=>2]);
        if ($save) {
            return $this->ajaxReturn(0, '下架成功', null);
        } else {
            return $this->ajaxReturn(3, '下架失败', null);
        }
    }
}
